<template>
  <div class="home-container">
    <el-container>
      <el-header>
        <div class="home-header-leftBox">
          <img src="../../assets/logo.png" alt="">
          <span>后台管理系统</span>
        </div>
        <el-button @click = "logout">退出</el-button>
      </el-header>
      <el-container>
        <el-aside :width=" isCollapse ? '60px' : '200px'">
          <div class="toggleMenu" @click="tagText" ref="shrink" :style="isCollapse ? baseStyle : '' ">←←</div>
          <el-menu
            :default-active="$route.path"
            class="el-menu-vertical-demo"
            background-color="#373d41"
            text-color="#fff"
            :unique-opened="true"
            :collapse="isCollapse"
            :collapse-transition="false"
            router
            active-text-color="#409eff">
            <!-- 一级菜单 -->
            <el-submenu :index="item.id.toString()" v-for="(item) in menuList" :key="item.id" >
              <template slot="title">
                <i :class="iconList[item.id]"></i>
                <span>{{item.authName}}</span>
              </template>
              <!-- 二级菜单 -->
               <el-menu-item :index=" '/' + childrenItem.path" v-for="childrenItem in item.children" :key="childrenItem.id">
                   <template slot="title">
                    <i class="el-icon-menu"></i>
                    <span>{{childrenItem.authName}}</span>
                  </template>
               </el-menu-item>
            </el-submenu>
          </el-menu>
        </el-aside>
        <el-main>
          <router-view></router-view>
        </el-main>
     </el-container>
</el-container>
  </div>
</template>

<script>
export default {
  data () {
    return {
      menuList: [],
      iconList: {
        125: 'iconfont icon-users',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      },
      isCollapse: false,
      baseStyle: {
        width: '60px'
      }
    }
  },
  created () {
    this.getMenuList()
  },
  methods: {
    tagText () {
      this.isCollapse = !this.isCollapse
      if (this.isCollapse) {
        this.$refs.shrink.innerHTML = '→→'
      } else {
        this.$refs.shrink.innerHTML = '←←'
      }
    },
    logout () {
      sessionStorage.removeItem('token')
      this.$router.push('/login')
      this.$message('退出成功')
    },
    async getMenuList () {
      const { data: res } = await this.$axios.get('menus')
      this.menuList = res.data
    }
  }
}
</script>

<style scoped lang="less">
  .home-container {
    width: 100%;
    height: 100%;
    .el-container {
      height: 100%;
      .el-header {
        display:flex;
        background-color: rgb(51, 46, 46);
        justify-content:space-between;
        align-items: center;
       .home-header-leftBox {
         display: flex;
         align-items:center;
          img {
           width: 40px;
           margin-left: 20px;
         }
         span{
           font-size: 25px;
           color: #fff;
           margin-left: 20px;
         }
       }
      }
      .el-aside{
         background-color:#373d41;
         .iconfont{
           margin-right: 8px;
         }
         .el-menu{
           border: none;
         }
         .toggleMenu{
           width: 200px;
           height: 40px;
           background-color:rgba(0,0,0,0.5);
           color: #fff;
           text-align: center;
           font-size: 20px;
           font-weight: 1000;
           line-height: 40px;
           user-select: none;
           cursor: pointer;
         }
      }
      .el-main {
        background-color: #eee;
      }
      .el-submenu{
        border-bottom: 1px solid #999;
        }
    }
  }
</style>
